package com.guigui.gulimall.member.controller;

import com.guigui.common.constant.BizCodeConstant;
import com.guigui.common.utils.PageUtils;
import com.guigui.common.utils.R;
import com.guigui.gulimall.member.dto.LoginDto;
import com.guigui.gulimall.member.dto.RegisterDto;
import com.guigui.gulimall.member.entity.MemberEntity;
import com.guigui.gulimall.member.exception.PhoneException;
import com.guigui.gulimall.member.exception.UsernameException;
import com.guigui.gulimall.member.feign.CouponFeignService;
import com.guigui.gulimall.member.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 会员
 *
 * @author xin
 * @email 806748502@qq.com
 * @date 2021-07-23 16:09:19
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    CouponFeignService couponFeignService;


    @GetMapping("/coupon/list")
    public R feignTest() {
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setNickname("张三");
        return R.ok().put("member", memberEntity).put("coupon", couponFeignService.memberList());
    }

    @PostMapping("/register")
    public R register(@RequestBody RegisterDto registerDto) {
        try {
            memberService.register(registerDto);
        } catch (PhoneException | UsernameException e) {
            if (e instanceof PhoneException) {
                return R.error(BizCodeConstant.ErrorCodeStatus.PHONE_UNIQUE_ERR.getCode(), e.getMessage());
            }
            return R.error(BizCodeConstant.ErrorCodeStatus.USERNAME_UNIQUE_ERR.getCode(), e.getMessage());
        }
        return R.ok();
    }

    @PostMapping("/login")
    public R login(@RequestBody LoginDto loginDto) {
        MemberEntity user;
        try {
            user = memberService.login(loginDto);
        } catch (RuntimeException e) {
            return R.error(BizCodeConstant.ErrorCodeStatus.LOGIN_ERR.getCode(), e.getMessage());
        }
        return R.ok().put("data", user);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
